<template>
  <el-dialog v-model="visible" width="960px" append-to-body lock-scroll class="batch-print-dialog">
    <template #header>
      <!-- Header -->
      <div class="header-strip">
        <div class="strip-left">
          <el-icon>
            <Printer />
          </el-icon>
          <span>批量生成单号</span>
        </div>
        <el-tag size="small" effect="dark">共 {{ orders.length }} 单</el-tag>
      </div>
    </template>
    <!-- Orders list -->
    <div class="order-list">
      <div v-for="order in orders" :key="order.id" class="order-card">
        <div class="order-header">
          <div class="order-no">
            订单号：<strong>{{ order.orderNo }}</strong>
            <span v-if="order.isMultiPackage == '1'">（多包裹）</span>
          </div>
          <div class="customer">
            <el-tag type="info" size="small">{{ order.receiverName }}</el-tag>
            <span class="phone">{{ order.receiverPhone }}</span>
          </div>
        </div>
        <div class="order-meta">
          <span>下单日期：{{ order.orderTime }}</span>
          <span>金额：¥{{ order.orderPrice?.toFixed(2) }}</span>
        </div>
        <div class="remark">
          <el-icon>
            <ChatLineSquare />
          </el-icon>
          <el-input v-model="order.remark" placeholder="无备注" size="small" clearable />
        </div>
      </div>
    </div>

    <!-- Footer -->
    <template #footer>
      <div class="footer-bar">
        <span class="summary">
          共 {{ orders.length }} 单，总金额 ¥{{ totalAmount.toFixed(2) }}
        </span>
        <div class="actions">
          <el-button @click="close">取消</el-button>
          <el-button type="primary" :icon="Printer" @click="print">确认打印</el-button>
        </div>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive, computed, nextTick } from 'vue';
import { Printer, ChatLineSquare } from '@element-plus/icons-vue';
import useUserStore from '../../store/modules/user';
import { applyRemarks } from '../../utils/order';
const userApp = useUserStore();

const visible = ref(false);
const orders = reactive<any[]>([]);

const totalAmount = computed(() => orders.reduce((sum, o) => sum + o.orderPrice, 0));

const show = async (list: any[]) => {
  applyRemarks(list, userApp.printStr);
  orders.splice(0, orders.length, ...list);
  visible.value = true;
  await nextTick();
};

const close = () => {
  visible.value = false;
};

// 出单
const print = () => {
  close();
};

defineExpose({ show, close });
</script>

<style lang="scss" scoped>
.batch-print-dialog :deep(.el-dialog__header) {
  display: none;
}

.header-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-radius: 4px;

  .strip-left {
    display: flex;
    gap: 8px;
    align-items: center;
    color: #64748b;
    font-weight: 600;
  }

  .strip-right {
    font-size: 12px;
    color: #64748b;
  }
}

.remark-config {
  background: #f9fafb;
  padding: 12px 16px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.order-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(420px, 1fr));
  gap: 16px;
  max-height: 540px;
  overflow-y: auto;
  padding-right: 4px;
}

.order-card {
  background: #fff;
  border-radius: 4px;
  padding: 14px 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  transition: all 0.2s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  }

  .order-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;

    .order-no {
      font-weight: 600;
    }

    .customer {
      display: flex;
      align-items: center;
      gap: 8px;
    }

    .phone {
      font-size: 13px;
      color: #64748b;
    }
  }

  .order-meta {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
    color: #475569;
    margin-bottom: 6px;
  }

  .remark {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: #6b7280;
  }
}

.footer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 8px;
  border-top: 1px dashed #e5e7eb;

  .summary {
    font-size: 14px;
    color: #374151;
  }

  .actions {
    display: flex;
    gap: 8px;
  }
}
</style>
